# filter companies for blue-green deployment

=begin

files:
proxy_access.log.2020-08-28-15.sh-api-gw002.gz
proxy_access.log.2020-08-28-15.sh-api-gw003.gz

bash commands

# zgrep -Eo ' "[[:digit:]]+" ' proxy_access.log.2020-08-28-15.sh-api-gw002.gz | grep -Eo '[[:digit:]]+' > /tmp/tids_stat_2020_08_28_002.log
# zgrep -Eo ' "[[:digit:]]+" ' proxy_access.log.2020-08-28-15.sh-api-gw003.gz | grep -Eo '[[:digit:]]+' > /tmp/tids_stat_2020_08_28_003.log

# cat tids_stat_2020_08_28_002.log tids_stat_2020_08_28_003.log > /tmp/tids_stat_0828.log

# wc -l /tmp/tids_stat_0828.log   # 6484426

# sort -n /tmp/tids_stat_0828.log | uniq -c | sort -nr > /tmp/tids_stat_0828_freq.log

=end


def get_candidates(file_name:, total: )
  freq_by_tid = File.foreach(file_name).each_with_object({}) do |line, h|
    v, k = line.chomp.lstrip.split(" ").map(&:to_i)
    h[k] = v
  end

  ratio_by_tid = freq_by_tid.each_with_object({}) do |(k, v), h|
    h[k] = v.fdiv(total)
  end

  hash = ratio_by_tid.select { |_, v| v > 0.0001 }
  hash = hash.reject { |k, _| k == 0 }
  candidates = hash.reject { |k, _| block_ids.include?(k) }

  detailed_candidates = candidates.each_with_object({}) do |(tid, ratio), h|
    on_shard_by_company_id(tid) do
      com = Company.find(tid)
      h[tid] = {}
      h[tid]["subdomain"] = com.subdomain
      h[tid]["ratio"] = ratio * 100
      h[tid]["shard_id"] = ActiveRecord::Base.current_shard_id
      h[tid]["company_name"] = com.company_name
      h[tid]["agent_count"] = com.agents.count
      h[tid]["tid"] = tid
    end
  end

  de = detailed_candidates.select { |_, v| v['agent_count'] < 50 }
  de = de.select { |_, v| v['ratio'] > 0.02 }

  de.values.each do |h|
    puts "#{h['tid']},#{h['subdomain']},#{h['shard_id']},#{h['company_name']},#{h['ratio']},#{h['agent_count']}"
  end

  first_10_percent_tenants = select_first_10_percent(de)

  puts "First 10 percent:"
  print_list(first_10_percent_tenants)

  remains = de.reject { |k, v| first_10_percent_tenants.map { |v| v['tid'] }.include?(k) }
  another_40_percent_tenants = select_40_percent(remains)

  puts "another 40 percent:"
  print_list(another_40_percent_tenants)

end

def select_40_percent(de)
  results = []

  de.reject { |k, v| v['ratio'] < 2 && v['ratio'] > 0.1 }.values.each do |h|
    if results.inject(0) { |r, h| r + h["ratio"] } <= 40
      results << h
    end
  end
  results
end

def select_first_10_percent(de)
  results = []
  de.reject { |k, _| huge_traffic_ids.include?(k) }.values.first(30).shuffle.each do |h|
    if results.inject(0) { |r, h| r + h["ratio"] } <= 10
      results << h
    end
  end
  results
end

def print_list(array)
  puts
  puts "----"

  array.each do |h|
    puts "#{h['tid']},#{h['subdomain']},#{h['shard_id']},#{h['company_name']},#{h['ratio']},#{h['agent_count']}"
  end

  puts "----"
  puts
end

def key_customer_subdomains
  %w(
    xinliu
    oristartech
    jiangroom
    4px
    kuaishouhr
    feihe
    relxtech
    oyohotels
    consumerservice
    wxbio
    kaipujushi
    jiahui
    mengniu
    zejipx
    1396599
    sce
    1349541
    hyfxdh
    hyxfzy
    allegion
    peacebird
    yingdajincailvxingshe
    wens
    xtransfer
    zbom
    1359004
  )
end

def key_ids
  on_shard(1) { Company.where(subdomain: key_customer_subdomains).pluck(:id) }
end

def special_ids
  [1, 7]
end

def block_ids
  key_ids + special_ids + [13640, 4874]
end

def huge_traffic_ids
  # 13640, 1361514, 13.2% traffic ratio, 上海韩众网络科技有限公司/上海摄图
  # 4874, dzyxedu, 5.66% traffic ratio, 河南高辉教育科技有限公司/河南省华辉计算机
  # 26468, 1520026, xx% 企查查科技有限公司
  [26468]
end

get_candidates(file_name: "/tmp/freq.log", total: 10316919)
